//
//  Advertising.swift
//  iBeacon
//
//  Created by 郭颢源 on 2021/4/8.
//

import UIKit
import CoreBluetooth
import CoreLocation
let beaconIdentifier = "testBeacon"
/// 广播
class Advertising: UIViewController,CBPeripheralManagerDelegate {
    
    fileprivate var beaconRegion:CLBeaconRegion!
    fileprivate var beaconPeripheralData: NSDictionary!
    fileprivate var peripheraManager: CBPeripheralManager!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        beaconRegion = CLBeaconRegion(uuid: UUID(uuidString: "174232B6-F53E-49DA-A32C-224016982580")!, major: 1, minor: 2, identifier: beaconIdentifier)
        beaconPeripheralData = beaconRegion.peripheralData(withMeasuredPower: nil)
        
        peripheraManager = CBPeripheralManager(delegate: self, queue: nil)
        
        
    }
    
    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        /**
         case unknown = 0
         
         case resetting = 1
         
         case unsupported = 2
         
         case unauthorized = 3
         
         case poweredOff = 4
         
         case poweredOn = 5
         */
        switch peripheral.state {
        
        case .poweredOn:
            peripheraManager.startAdvertising(beaconPeripheralData as? [String : Any])
            print(beaconRegion.proximityUUID)
            //            print(beaconRegion.major)
            //            print(beaconRegion.minor)
            print(beaconRegion.identifier)
        default: peripheraManager.stopAdvertising()
        }
    }
    
    
    
    
    
}
